﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.Utils;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 滚动加载分页控件
    /// </summary>
    [System.ComponentModel.Designer(typeof(GridStandardPagingDesigner))]
    public partial class GridControlInfinitePaging : BaseUserControl
    {
        private bool _dataLoading;
        private PagingInfo _pagingInfo;

        /// <summary>
        /// 默认每页大小
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public static int DefaultPageSize { get; set; } = 100;

        /// <summary>
        /// 每页大小,0表示使用全局默认值
        /// </summary>
        [Category("XCI"), Description("每页大小"), DefaultValue(0)]
        public int PageSize { get; set; }

        /// <summary>
        /// 分页信息位置
        /// </summary>
        [Category("XCI"), Description("分页信息位置"), DefaultValue(PageInfoPositionEnum.Right)]
        public PageInfoPositionEnum PageInfoPosition { get; set; } = PageInfoPositionEnum.Right;

        /// <summary>
        /// 页数,从1开始
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int PageIndex => _pagingInfo?.PageIndex ?? 1;

        /// <summary>
        /// 总记录数
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int Total => _pagingInfo?.TotalCount ?? 0;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public PagingInfo CurrentPagingInfo => _pagingInfo;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public BasePageFilter CurrentPageFilter => new BasePageFilter(PageIndex, PageSize);

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool DataLoading
        {
            get => _dataLoading;
            set
            {
                _dataLoading = value;
                // labPageInfo.Text = "正在加载...";
            }
        }

        [Category("XCI"), Description("自定义分页显示消息")]
        public event EventHandler<FormatPagingMessageEventArgs> FormatPagingMessage;

        public GridControlInfinitePaging()
        {
            InitializeComponent();
            if (DesignMode)
            {
                this.Dock = DockStyle.Bottom;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!WinFormHelper.IsDesignMode())
            {
                if (PageSize == 0) PageSize = DefaultPageSize;

                labPageInfo.Text = string.Empty;
                this.labPageInfo.AutoSizeMode = DevExpress.XtraEditors.LabelAutoSizeMode.None;
                this.labPageInfo.Size = new System.Drawing.Size(this.Width, this.Height);
                this.labPageInfo.Padding = new System.Windows.Forms.Padding(5);
                this.labPageInfo.Dock = System.Windows.Forms.DockStyle.Fill;
                this.labPageInfo.Appearance.Options.UseTextOptions = true;
                this.labPageInfo.Appearance.TextOptions.VAlignment = VertAlignment.Center;
                switch (PageInfoPosition)
                {
                    case PageInfoPositionEnum.Left:
                        this.labPageInfo.Appearance.TextOptions.HAlignment = HorzAlignment.Near;
                        break;
                    case PageInfoPositionEnum.Right:
                        this.labPageInfo.Appearance.TextOptions.HAlignment = HorzAlignment.Far;
                        break;
                }
            }
        }

        public void BindingInfo(PagingInfo info)
        {
            _pagingInfo = info;
            if (FormatPagingMessage == null)
            {
                labPageInfo.Text = GetDefaultPagingMessage(_pagingInfo);
            }
            else
            {
                var args = new FormatPagingMessageEventArgs { PagingInfo = _pagingInfo };
                FormatPagingMessage.Invoke(this, args);
                labPageInfo.Text = string.IsNullOrEmpty(args.PagingMessage) ? GetDefaultPagingMessage(_pagingInfo) : args.PagingMessage;
            }
        }

        private string GetDefaultPagingMessage(PagingInfo info)
        {
            if (info == null) info = new PagingInfo(1, PageSize, 0);
            return info.TotalCount == 0 ? $"共 {info.TotalCount} 条记录" : $"已加载 {info.RecordEndIndex} 条记录,共 {info.TotalCount} 条记录";
        }

        //private void GridControlInfinitePaging_Paint(object sender, PaintEventArgs e)
        //{
        //    if (DesignMode) return;
        //    var isDarkSkin = DevExpress.Utils.Frames.FrameHelper.IsDarkSkin(UserLookAndFeel.Default);
        //    if (pen == null) pen = isDarkSkin ? new Pen(Color.FromArgb(75, 75, 75)) : new Pen(Color.FromArgb(200, 200, 200));
        //    e.Graphics.DrawLine(pen, 0, 0, Width, 0);
        //}
    }

    public enum PageInfoPositionEnum
    {
        Left,
        Right
    }
    
    public class LoadInfinitePagingDataEventArgs : EventArgs
    {
        /// <summary>
        /// 分页请求信息
        /// </summary>
        public BasePageFilter PageFilter { get; set; }
    
        public Func<Task<IPagingList>> DataSourceTask { get; set; }
    }
}
